package com.yyfax.ph.personal.authority.verifycode;

import com.yyfax.common.tools.MD5;
import com.yyfax.common.CLog;
import com.yyfax.common.tools.TimeUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.HeaderMap;
import retrofit2.http.POST;
import rx.Observable;

import java.util.HashMap;
import java.util.Map;

/**
* M层:获取、校验验证码
* @author ying_he
* @date 2017/3/7 17:43
*/
public class VerifyCodeModel extends Model {
    private static final String TAG = VerifyCodeModel.class.getSimpleName();

    protected static final int MSG_OBTAIN_VERIFY_CODE = 6;
    protected static final int MSG_CHECK_VERIFY_CODE = 7;

    // 用户注册
    public static final String VERIFY_CODE_TYPE_USER_REGISTER = "ur";
    // 忘记密码
    public static final String VERIFY_CODE_TYPE_FORGET_PWD = "fp";
    // 推荐
    public static final String VERIFY_CODE_TYPE_RECOMMEND = "rc";
    // 修改手机号
    public static final String VERIFY_CODE_TYPE_MODIFY_PHONE_NUM = "mp";
    // 绑定
    public static final String VERIFY_CODE_TYPE_BIND = "bd";

    @Override
    public Observable getObservable(Object params, int functionalCode) {
        Service service = (Service) NetworkHelper.createService(Service.class);
        String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
        switch (functionalCode) {
            case MSG_OBTAIN_VERIFY_CODE:
                Map<String, String> header = new HashMap<>();
                String currentTime = TimeUtils.getCurrentTimeInString();
                header.put("Verify-Key", MD5.encrypt("YJS_VERIFY#" + currentTime).toLowerCase());
                header.put("timestamp", currentTime);
                header.put("token", token);
                return service.obtainVerifyCode(header, (VerifyCodeParams) params);
            case MSG_CHECK_VERIFY_CODE:
                return service.checkVerifyCode(token, (VerifyCodeParams) params);
            default:
                break;
        }
        return null;
    }

    @Override
    public void onNext(Object obj) {
        CLog.i(TAG, "onNext: " + obj.toString());
        BaseResultEntity result = cast(obj);
        switch (result.requestCode) {
            case MSG_OBTAIN_VERIFY_CODE:
                ObtainVerifyCodeResult obtainVerifyCodeResult = cast(obj);
                EventAgent.post(obtainVerifyCodeResult);
                break;
            case MSG_CHECK_VERIFY_CODE:
                CheckVerifyCodeResult checkVerifyCodeResult = cast(obj);
                EventAgent.post(checkVerifyCodeResult);
                break;
            default:
                break;
        }
    }

    private interface Service {
        @POST("sys/smscode/send")
        Observable<ObtainVerifyCodeResult> obtainVerifyCode(@HeaderMap Map<String, String> header, @Body VerifyCodeParams params);
        @POST("sys/smscode/validate")
        Observable<CheckVerifyCodeResult> checkVerifyCode(@Header("token") String token, @Body VerifyCodeParams params);
    }

    public static class VerifyCodeParams {
		/***
		 *手机,RSA加密
		 */
		public String phone;
		/***
		 *验证码 长度:6
		 */
        public String type;
		/***
		 * 类型，参考发送时的验证码类型   ur:用户注册,fp:忘记密码,rc:推荐,mp:修改手机号,bd:绑定
		 */
		public String code;
    }

    static class ObtainVerifyCodeResult extends BaseResultEntity<ObtainVerifyCodeContentBean> {}
    static class CheckVerifyCodeResult extends BaseResultEntity<CheckVerifyCodeContentBean> {}

    class ObtainVerifyCodeContentBean{
		/***
		 *验证码，仅在开发时有返回，生产不会返回
		 */
		public String code;
    }

    class CheckVerifyCodeContentBean{
		/***
		 *校验订单号
		 */
		protected String cid;
		/***
		 * code提示信息 10008:验证码错误
		 */
        protected String code;
    }
}
